//
//  PasswordViewController.m
//  AipuTel
//
//  Created by cretve on 15/1/15.
//  Copyright (c) 2015年 aipu. All rights reserved.
//

#import "PasswordViewController.h"
#import "AESCrypt.h"

@interface PasswordViewController ()

@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UITextField *origPasswordField;
@property (weak, nonatomic) IBOutlet UITextField *passwordField;

@end

@implementation PasswordViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"修改密码";
    
    _origPasswordField.bk_shouldReturnBlock = ^BOOL(UITextField* textField){
        [textField resignFirstResponder];
        return YES;
    };
    
    _passwordField.bk_shouldReturnBlock = ^BOOL(UITextField* textField){
        [textField resignFirstResponder];
        return YES;
    };
    
    NSDictionary* userInfo = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"UsreInfo"];
    _phoneField.text = userInfo[@"username"];
}

- (IBAction)clickedSubmit:(id)sender
{
    if(_origPasswordField.text.length < 6 || _origPasswordField.text.length > 22)
    {
        [UIAlertView bk_showAlertViewWithTitle:@"请输入正确的旧密码" message:nil cancelButtonTitle:@"好" otherButtonTitles:nil handler:nil];
        return;
    }
    
    if(_passwordField.text.length < 6 || _passwordField.text.length > 22)
    {
        [UIAlertView bk_showAlertViewWithTitle:@"新密码长度不符" message:nil cancelButtonTitle:@"好" otherButtonTitles:nil handler:nil];
        return;
    }
    
    [_origPasswordField resignFirstResponder];
    [_passwordField resignFirstResponder];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"请稍候...";
    
    __weak PasswordViewController* weakSelf = self;
    [[AFHTTPRequestOperationManager manager] POST:@"http://202.98.133.196/ddserver/chgpwd" parameters:@{@"username": _phoneField.text, @"newpassword": [AESCrypt encrypt:_passwordField.text], @"oldpassword": [AESCrypt encrypt:_origPasswordField.text]} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
        
        if([responseObject[@"errno"] integerValue] == 0)
        {
            [[NSUserDefaults standardUserDefaults] setObject:@{@"username": weakSelf.phoneField.text, @"password": weakSelf.passwordField.text} forKey:@"UserAccount"];
            
            [UIAlertView bk_showAlertViewWithTitle:@"修改成功" message:nil cancelButtonTitle:@"好" otherButtonTitles:nil handler:nil];
            
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
        else
        {
            [UIAlertView bk_showAlertViewWithTitle:responseObject[@"info"] message:nil cancelButtonTitle:@"好" otherButtonTitles:nil handler:nil];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
        [UIAlertView bk_showAlertViewWithTitle:NetworkErrorMessage message:nil cancelButtonTitle:@"好" otherButtonTitles:nil handler:nil];
    }];
}

@end
